﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.user.Edit_Entities
{
    public partial class EditComponent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorLabel.Visible = false;
            SentLabel.Visible = false;

        }

        protected void ComponentDetailsView_DataBound(object sender, EventArgs e)
        {
            //George , in the item template , i changed the label names
            //so it would be easier to find them
            Label vendor = (Label)ComponentDetailsView.FindControl("VendorLabel");
            Label supplier = (Label)ComponentDetailsView.FindControl("SupplierLabel");
            if (vendor != null)
            {
                VendorBLL vendorBLL = new VendorBLL(); ;
                SupplierBLL supplierBLL = new SupplierBLL();
                try
                {
                    supplier.Text = supplierBLL.SupplierGetByNum(new Guid(supplier.Text))[0].supplier_name;
                    vendor.Text = vendorBLL.VendorGetByNum(new Guid(vendor.Text))[0].vendor_name;
                }
                finally
                {
                    //TODO
                    //we should add dispose here
                    // we need to add dispose function foreach bll
                    // or maybe the detailsview disposes alone ,
                    // need to check, because there exists disposing event
                }
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {

            ComponentBLL componentBLL = new ComponentBLL();
            try
            {
                if (componentBLL.ComponentGetBySN(args.Value).Rows.Count > 0)
                    args.IsValid = false;
                else
                    args.IsValid = true;
            }
            finally
            {
            }

        }

        protected void CustomValidator1_ServerValidate1(object source, ServerValidateEventArgs args)
        {
            ComponentBLL BLL = new ComponentBLL();
            String Previous = BLL.ComponentGetByNum((Guid)ComponentDetailsView.SelectedValue)[0].component_serial_num;


            if (Previous != args.Value)
            {
                try
                {
                    if (BLL.ComponentGetBySN(args.Value).Rows.Count > 0)
                        args.IsValid = false;
                    else
                        args.IsValid = true;
                }
                finally
                {
                    //TODO: Dispose
                }

            }
        }

        protected void ComponentDetailsView_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            try
            {
                Label Label = (Label)ComponentDetailsView.FindControl("ComponentLabel");
                ComponentBLL BLL = new ComponentBLL();
                Guid guid = BLL.ComponentGetBySN(Label.Text)[0].component_num;


                DeliveredBLL DBLL = new DeliveredBLL();
                if (DBLL.DeliveredGetByComponent(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;
                }


            }
            finally
            {
                //TODO: Dispose
            }
        }

        protected void ComponentDetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            MailPanel1.Visible = false;
            ErrorLabel.Visible = false;
            SentLabel.Visible = false;
            if (e.CommandName == "MailComponent")
            {
              
                CustomerRadioButton.Enabled = true;
                SupplierRadioButton.Enabled = true;
                CustomerComboBox.Enabled = true;
                SentLabel.Visible = false;
                NotesTextBox.Text = "";
                NotesTextBox.Enabled = true;
                SendButton.Enabled = true;
                if (MailPanel1.Visible == false)
                {
                    MailPanel1.Visible = true;
                    SendButton.Focus();
                }
                else MailPanel1.Visible = false;
                ComponentDetailsView.ChangeMode( DetailsViewMode.ReadOnly);
            }
        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            try
            {
                SentLabel.Focus();
                MailPanel1.Visible = false;
                ComponentBLL BLL = new ComponentBLL();
                VendorBLL VBLL = new VendorBLL();
                SupplierBLL SBLL = new SupplierBLL();
                string EmailAddress;
                string Subject = "Entity Details have been sent to you by The WareHouse";
                Guid Key = new Guid(ComponentDetailsView.DataKey.Value.ToString());
                String ComponentSerial = BLL.ComponentGetByNum(Key)[0].component_serial_num;
                String ComponentDesc = BLL.ComponentGetByNum(Key)[0].component_description;
                String ComponentVendor = VBLL.VendorGetByNum(BLL.ComponentGetByNum(Key)[0].component_vendor)[0].vendor_name;
                String ComponentSupplier = SBLL.SupplierGetByNum(BLL.ComponentGetByNum(Key)[0].component_supplier)[0].supplier_name;
                String ComponentQuantity = BLL.ComponentGetByNum(Key)[0].component_quantity.ToString();

                String[] fields = { "Component Serial", "Component Description", "Component Vendor", "Component Supplier","Component Quantity" };
                String[] values = { ComponentSerial, ComponentDesc, ComponentVendor, ComponentSupplier, ComponentQuantity };

                String EntityString = Mail.HTMLEntityDecorator("Component Details", fields, values);
                if (SupplierRadioButton.Checked)
                {
                    string SupplierMail = SBLL.SupplierGetByNum(BLL.ComponentGetByNum(Key)[0].component_supplier)[0].supplier_email;
                    EmailAddress = SupplierMail;

                }
                else
                {
                    CustomerBLL CBLL = new CustomerBLL();
                    string CustomerMail = CBLL.CustomerGetByNum(new Guid(CustomerComboBox.SelectedValue))[0].customer_email;
                 
                    EmailAddress = CustomerMail;
                }

                Boolean res = Mail.SendMail(EmailAddress, Subject, Mail.SenderNotes(NotesTextBox.Text)+EntityString );
                if (res == false)
                {
                    SentLabel.Text = "Mail Failed";
                }
            }

            catch (Exception)
            {
                SentLabel.Text = "Mail Failed";

            }

            finally
            {
                //TODO:Dispose;
                CustomerRadioButton.Enabled = false;
                SupplierRadioButton.Enabled = false;
                NotesTextBox.Enabled = false;
                SendButton.Enabled = false;
                CustomerComboBox.Enabled = false;
                SentLabel.Visible = true;
                MailPanel1.Visible = false;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SentLabel.Visible = false;
            MailPanel1.Visible = false;
        }

    }
}
